/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package service;

import graphviewer.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

/**
 *
 * @author Sabitov Artem
 * @date 02.10.2013
 */
public class ServiceLog extends JPanel implements MouseWheelListener {
	private static String[] log = null;
	private static ArrayList<String> logList = null;
	private int sizeLog = 31;
	private int first = 0;
	private int last = 0;
	
	private int posX = 4;
	private int posY = 0;
	private String sLog = "";
	private String sDbgLog = "state log: ";
	private String sLogState;
	private int step = 14;
	private boolean fullLog;
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		Font font = new Font("Arial", 0, 11);
		g2d.setFont(font);
		g2d.drawString(sDbgLog + sLogState, posX, posY + step);
		g2d.drawLine(posX, posY + step + 3, posX + GraphViewer.FRAME_WIDTH / 4, posY + step + 3);
		
		if(!fullLog) {
			for(int i = 0; i < log.length; i++) {
				g2d.drawString(sLog + log[i], posX, posY + step + step + step * i);
				g2d.drawLine(posX, posY + step*(i+2) + 3, posX + GraphViewer.FRAME_WIDTH / 4, posY + step*(i+2) + 3);
			}
		} else {
			for(int i = first, j = 0; i < last && j < sizeLog; i++, j++) {
				g2d.drawString(sLog + logList.get(i), posX, posY + step + step + step * j);
				g2d.drawLine(posX, posY + step*(j+2) + 3, posX + GraphViewer.FRAME_WIDTH / 4, posY + step*(j+2) + 3);
			}
		}
		setBounds(0, 250, 236, 462);
	}
	
	public ServiceLog(boolean fullLog) {
		this.fullLog = fullLog;
		log = new String[sizeLog];
		logList = new ArrayList<>();
		setBackground(Color.YELLOW);
		setOpaque(Service.getStateDebugComponent());
//		if(Service.getLogType()) {
			addMouseListener();
//		}
		if(Service.getServiceState()) {
			sLogState = "enable";
		} else {
			sLogState = "disable";
		}
		log("----------------------------------------------------------");
	}
	
	// #remake
	protected final void log(Object obj) {
//		System.out.println(obj.toString());
		if(!fullLog) {
			if(Service.getServiceState()) {
				if(log != null) {
					if(last < sizeLog) {
						log[last] = obj.toString();
						if(last < sizeLog) last++;
					} else if(last == sizeLog) {
						for(int i = 0; i < last - 1; i++) {
							log[i] = log[i + 1];
						}
						log[last - 1] = obj.toString();
					}
				}
			}
		} else {
			if(Service.getServiceState()) {
				if(!(logList.size() > 8192)) {
					logList.add(obj.toString());
					last++;
					if(last > sizeLog) {
						first = last - sizeLog;
					}
				}
			}
		}
		update();
	}
	
	private void addMouseListener() {
		this.addMouseWheelListener(this);
	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent mwe) {
		int notches = mwe.getWheelRotation();
		if(notches < 0) {
			if(first > 0) {
				first -= 1;
				last = first + sizeLog;
			}
		} else {
			if(last < logList.size()) {
				first += 1;
				last = first + sizeLog;
			}
		}
		update();
	}
	
	public void update() {
		repaint();
	}
}



